package org.xxpay.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xxpay.core.entity.SysLog;
import org.xxpay.core.entity.SysLogExample;
import org.xxpay.core.service.ISysLogService;
import org.xxpay.service.dao.mapper.SysLogMapper;

import java.util.List;

@Service
public class SysLogServiceImpl implements ISysLogService {
    
    @Autowired
    private SysLogMapper recordMapper;

    void setCriteria(SysLogExample.Criteria criteria, SysLog obj, JSONObject queryObj) {
        if(obj != null) {
            if(obj.getUserId() != null) criteria.andUserIdEqualTo(obj.getUserId());
            if(obj.getSysType() != null) criteria.andSysTypeEqualTo(obj.getSysType());
            if(StringUtils.isNotEmpty(obj.getUserName())) criteria.andUserNameEqualTo(obj.getUserName());
            if(StringUtils.isNotEmpty(obj.getUserIp())) criteria.andUserIpEqualTo(obj.getUserIp());
            if(StringUtils.isNotEmpty(obj.getMethodRemark())) criteria.andMethodRemarkEqualTo(obj.getMethodRemark());
        }
        if(queryObj != null) {
            if(queryObj.getDate("createTimeStart") != null) criteria.andCreateTimeGreaterThanOrEqualTo(queryObj.getDate("createTimeStart"));
            if(queryObj.getDate("createTimeEnd") != null) criteria.andCreateTimeLessThanOrEqualTo(queryObj.getDate("createTimeEnd"));
        }
    }

    @Override
    public int add(SysLog record) {
        return recordMapper.insertSelective(record);
    }

    @Override
    public List<SysLog> select(int offset, int limit, SysLog record, JSONObject queryObj) {
        SysLogExample example = new SysLogExample();
        example.setOrderByClause("CreateTime desc");
        example.setOffset(offset);
        example.setLimit(limit);
        SysLogExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, record, queryObj);
        return recordMapper.selectByExample(example);
    }

    @Override
    public Integer count(SysLog record, JSONObject queryObj) {
        SysLogExample example = new SysLogExample();
        SysLogExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, record, queryObj);
        return recordMapper.countByExample(example);
    }
}
